package com.smart.parking.service.impl;


import com.smart.parking.entity.ChargeEntity;
import com.smart.parking.param.AliPay;
import com.smart.parking.service.ChargeService;
import com.smart.parking.service.SpaceService;
import com.smart.parking.service.VehicleService;
import com.smart.parking.utils.PageUtils;
import com.smart.parking.utils.Query;
import com.smart.parking.vo.VehicleVO;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


import com.smart.parking.mapper.VehicleMapper;
import com.smart.parking.entity.VehicleEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;


@Service("vehicleService")
public class VehicleServiceImpl extends ServiceImpl<VehicleMapper, VehicleEntity> implements VehicleService {


    @Autowired
    private ChargeService chargeService;

    @Autowired
    private SpaceService spaceService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<VehicleEntity> page = this.page(
                new Query<VehicleEntity>().getPage(params),
                new QueryWrapper<VehicleEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 增加车辆
     *
     * @param vehicleVO 车辆实体
     * @return boolean
     */
    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Exception.class})
    public boolean addVehicle(VehicleVO vehicleVO) {
        //转换实体对象
        VehicleEntity vehicleEntity = new VehicleEntity();
        vehicleEntity.setVehiclePlate(vehicleVO.getVehiclePlate());
        try {
            //新增车辆信息
            int insert = baseMapper.insert(vehicleEntity);
            if (insert > 0) {
                spaceService.lotParking(vehicleVO.getSpaceId());
                VehicleEntity vehicleEntity1 = baseMapper.selectOne(new QueryWrapper<VehicleEntity>().eq("vehicle_plate", vehicleEntity.getVehiclePlate()));
                //新增一条收费记录
                boolean flag = chargeService.addCharge(vehicleEntity1);
                if (flag) {
                    return true;
                }
            }
            //新增失败，回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        } catch (Exception e) {
            //设置手动回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return false;
    }

    /**
     * 结束停车
     *
     * @param vehicleVO 车牌信息
     * @return {@link Double}
     */
    @Override
    public AliPay endVehicle(VehicleVO vehicleVO) {
        //释放车位
        spaceService.releaseParking(vehicleVO.getSpaceId());
        VehicleEntity vehicleEntity = baseMapper.selectOne(new QueryWrapper<VehicleEntity>().eq("vehicle_plate", vehicleVO.getVehiclePlate()));
        return chargeService.calculationPrice(vehicleEntity);
    }

    /**
     * 删除车辆信息
     *
     * @param vehiclePlate 车牌号
     */
    @Override
    public boolean delVehicle(String vehiclePlate) {
        int del = baseMapper.delete(new QueryWrapper<VehicleEntity>().eq("vehicle_plate", vehiclePlate));
        return del > 0;
    }

    /**
     * 手动删除车辆信息
     */
    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Exception.class}) //事务
    public void manualRelease(Long vehicleId) {
        try {
            VehicleEntity vehicleEntity = baseMapper.selectById(vehicleId);
            int vehicle = baseMapper.deleteById(vehicleId);
            if (vehicle > 0) {
                boolean remove = chargeService.remove(new QueryWrapper<ChargeEntity>().eq("vehicle_plate", vehicleEntity.getVehiclePlate()));
                if (remove) {
                    return;
                }
            }
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        } catch (Exception e) {
            //设置手动回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }

    }


    /**
     * 手动删除车辆
     *
     * @param vehicleIds 车辆标识
     */
    @Override
    public void manualReleases(Long[] vehicleIds) {
        for (Long vehicleId : vehicleIds) {
            this.manualRelease(vehicleId);
        }
    }

    /**
     * 查询是否支付
     * @param chargeID
     * @return
     */
    @Override
    public boolean selectPayment(Long chargeID) {
        ChargeEntity chargeEntity = chargeService.getById(chargeID);
        return chargeEntity.getPayment() == 1;
    }

    /**
     * 更新支付状态 删除车辆信息
     *
     * @param chargeID 费用id
     */
    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Exception.class}) //事务
    public void updateChargePlay(Long chargeID) {
        ChargeEntity chargeEntity = chargeService.getById(chargeID);
        chargeEntity.setPayment(1);
        chargeService.updateById(chargeEntity);
        try {
            boolean flag = this.delVehicle(chargeEntity.getVehiclePlate());
            if (flag) {
                return;
            }
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        } catch (Exception e) {
            //设置手动回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
    }

}
